package com.qf.controller;

import com.qf.util.CookieUtil;
import com.qf.util.JwtUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * 用户登录授权
 *
 * @version v1.0.0
 * @Description:
 * @author: 姜红凯
 * @date: 2022.03.25
 * @since 1.0
 */
@RestController
@RequestMapping("/auth")
public class LoginController {

    @Value("${custom.username}")
    private String name;

    @Value("${custom.password}")
    private String pwd;

    /**
     * 授权认证方法
     *
     * @param username
     * @param password
     * @param response
     * @return
     */

    @RequestMapping("/login")
    public String login(String username, String password, HttpServletResponse response) {
        if (!name.equals(username)) {
            return "用户名错误";
        }
        if (!pwd.equals(password)) {
            return "密码错误";
        }
        //生产jwt,超时时间为1小时
        String jwt = JwtUtil.createJWT(UUID.randomUUID().toString(), username, null);
        //将jwt写入到用户浏览器cookie中
        CookieUtil.addCookie(response, "localhost", "/", "token", jwt, -1, false);

        return "登录成功!";
    }
}

